import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

Rectangle {
    color: "#F0F7FF"
    anchors.top: parent.top
    anchors.bottom: parent.bottom
    anchors.topMargin: 30
    anchors.bottomMargin: 30
    anchors.right: parent.right
    anchors.rightMargin: 20
    anchors.left: parent.left
    anchors.leftMargin: 5
    radius: 10

    Theme { id: theme }

    FontLoader {
        id: iconFont
        source: "qrc:/assets/fontawesome-free-6.7.2-desktop/otfs/Font Awesome 6 Free-Solid-900.otf"
    }

    property var motorCheckModel: []
    property var temperatureCheckModel: []
    property var weightCheckModel: []
    property var phCheckModel: []
    property var conductivityCheckModel: []

    function initCheckBoxModel() {
        // 赋值数组
        alarmInfo.getAlarmParams();
        var data = alarmInfo.getSpeedParam();
        motorCheckModel = [
            { text: "rpm", defaultInput: data.ll, checked: data.llStatus },
            { text: "rpm", defaultInput: data.l, checked: data.lStatus },
            { text: "rpm", defaultInput: data.h, checked: data.hStatus },
            { text: "rpm", defaultInput: data.hh, checked: data.hhStatus },
        ];

        data = alarmInfo.getTemperatureParam();
        temperatureCheckModel = [
            { text: "℃", defaultInput: data.ll, checked: data.llStatus },
            { text: "℃", defaultInput: data.l, checked: data.lStatus },
            { text: "℃", defaultInput: data.h, checked: data.hStatus },
            { text: "℃", defaultInput: data.hh, checked: data.hhStatus },
        ];
        data = alarmInfo.getWeightParam();
        weightCheckModel = [
            { text: "kg", defaultInput: data.ll, checked: data.llStatus },
            { text: "kg", defaultInput: data.l, checked: data.lStatus },
            { text: "kg", defaultInput: data.h, checked: data.hStatus },
            { text: "kg", defaultInput: data.hh, checked: data.hhStatus },
        ];
        data = alarmInfo.getPhParam();
        phCheckModel = [
            { text: "", defaultInput: data.ll, checked: data.llStatus },
            { text: "", defaultInput: data.l, checked: data.lStatus },
            { text: "", defaultInput: data.h, checked: data.hStatus },
            { text: "", defaultInput: data.hh, checked: data.hhStatus },
        ];
        data = alarmInfo.getConductivityParam();
        conductivityCheckModel = [
            { text: "mS/cm", defaultInput: data.ll, checked: data.llStatus },
            { text: "mS/cm", defaultInput: data.l, checked: data.lStatus },
            { text: "mS/cm", defaultInput: data.h, checked: data.hStatus },
            { text: "mS/cm", defaultInput: data.hh, checked: data.hhStatus },
        ];
        recomputeTop();
    }

    function recomputeTop(){
        function isChecked(v){ return v === 1 || v === true }
        function all(idx){
            return isChecked(motorCheckModel[idx].checked) &&
                   isChecked(weightCheckModel[idx].checked) &&
                   isChecked(temperatureCheckModel[idx].checked) &&
                   isChecked(phCheckModel[idx].checked) &&
                   isChecked(conductivityCheckModel[idx].checked);
        }
        topCheckBox.model = [
            { text: "低低限", checked: all(0) ? 1 : 0 },
            { text: "低限",   checked: all(1) ? 1 : 0 },
            { text: "高限",   checked: all(2) ? 1 : 0 },
            { text: "高高限", checked: all(3) ? 1 : 0 }
        ];
    }

    onVisibleChanged: {
        if (visible){
            initCheckBoxModel();
        }
    }

    Component.onCompleted:{
        initCheckBoxModel();
    }

    // 顶部蓝色标题栏
    Rectangle {
        height: 90
        color: theme.focusColor
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.right: parent.right
        topLeftRadius: 10
        topRightRadius: 10

        ECheckBox {
            id: topCheckBox
            anchors.verticalCenter: parent.verticalCenter
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.leftMargin: 243
            showInputField: false
            spacingBetweenBoxAndText: 88
            uncheckedBoxColor: theme.focusColor
            boxBorderColor: "#FFFFFF"
            model: [
                { text: "低低限" , checked: 0 },
                { text: "低限" , checked: 0 },
                { text: "高限" , checked: 0 },
                { text: "高高限" , checked: 0 }
            ]
            onToggled: function(i, checked){
                function updateColumn(arr){
                    var copy = arr.slice()
                    copy[i] = {
                        text: arr[i].text,
                        defaultInput: arr[i].defaultInput,
                        checked: checked ? 1 : 0
                    }
                    return copy
                }
                motorCheckModel = updateColumn(motorCheckModel)
                weightCheckModel = updateColumn(weightCheckModel)
                temperatureCheckModel = updateColumn(temperatureCheckModel)
                phCheckModel = updateColumn(phCheckModel)
                conductivityCheckModel = updateColumn(conductivityCheckModel)
                recomputeTop()
            }
        }
    }

    // 主内容区
    Column {
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.topMargin: 150
        anchors.leftMargin: 0
        spacing: 55

        // 电机
        Row {
            spacing: 60
            Row {
                spacing: 15
                Rectangle {
                    width: 108; height: 108; radius: 9
                    color: "transparent"
                    Image {
                        source: "qrc:/assets/bjstir.png"
                        width: 108; height: 108
                        anchors.left: parent.left
                        anchors.top: parent.top
                        anchors.topMargin: 0
                        fillMode: Image.PreserveAspectFit
                    }
                }
                Text { 
                    text: "电机"; color: "#333333"; font.pixelSize: 28; font.bold: true
                    anchors.top: parent.top
                    anchors.topMargin: 25

                }
            }


            ECheckBox {
                id: motorCheckBox
                showInputField: true
                textColor: "#2E394C"
                spacingBetweenBoxAndText: 5
                Layout.fillWidth: true
                Layout.minimumWidth: 450
                model:motorCheckModel
                onToggled: recomputeTop()
            }
        }

        // 称重
        Row {
            spacing: 60
            Row {
                spacing: 15
                Rectangle {
                    width: 108; height: 108; radius: 9
                    color: "transparent"
                    Image {
                        source: "qrc:/assets/bjweight.png"
                        width: 108; height: 108
                        anchors.left: parent.left
                        anchors.top: parent.top
                        anchors.topMargin: 0
                        fillMode: Image.PreserveAspectFit
                    }
                }
                Text { 
                    text: "称重"; color: "#333333"; font.pixelSize: 28; font.bold: true
                    anchors.top: parent.top
                    anchors.topMargin: 25
                }
            }

            ECheckBox {
                id: weightCheckBox
                showInputField: true
                textColor: "#2E394C"
                spacingBetweenBoxAndText: 5
                Layout.fillWidth: true
                Layout.minimumWidth: 450
                model: weightCheckModel
                onToggled: recomputeTop()
            }
        }

        // 温度
        Row {
            spacing: 60
            Row {
                spacing: 15
                Rectangle {
                    width: 108; height: 108; radius: 9
                    color: "transparent"
                    Image {
                        source: "qrc:/assets/bjtemp.png"
                        width: 108; height: 108
                        anchors.left: parent.left
                        anchors.top: parent.top
                        anchors.topMargin: 0
                        fillMode: Image.PreserveAspectFit
                    }
                }
                Text { 
                    text: "温度"; color: "#333333"; font.pixelSize: 28; font.bold: true
                    anchors.top: parent.top
                    anchors.topMargin: 25
                }
            }

            ECheckBox {
                id: tempCheckBox
                showInputField: true
                textColor: "#2E394C"
                spacingBetweenBoxAndText: 6
                Layout.fillWidth: true
                Layout.minimumWidth: 450
                model: temperatureCheckModel
                onToggled: recomputeTop()
            }
        }

        // PH
        Row {
            spacing: 60
            Row {
                spacing: 15
                Rectangle {
                    width: 108; height: 108; radius: 9
                    color: "transparent"
                    Image {
                        source: "qrc:/assets/bjph.png"
                        width: 108; height: 108
                        anchors.left: parent.left
                        anchors.top: parent.top
                        anchors.topMargin: 0
                        fillMode: Image.PreserveAspectFit
                    }
                }
                Text { 
                    text: " PH "; color: "#333333"; font.pixelSize: 28; font.bold: true
                    anchors.top: parent.top
                    anchors.topMargin: 25
                }
            }

            ECheckBox {
                id: phCheckBox
                showInputField: true
                textColor: "#2E394C"
                spacingBetweenBoxAndText: 6
                Layout.fillWidth: true
                Layout.minimumWidth: 450
                model: phCheckModel
                onToggled: recomputeTop()
            }
        }

        // 电导
        Row {
            spacing: 60
            Row {
                spacing: 15
                Rectangle {
                    width: 108; height: 108; radius: 9
                    color: "transparent"
                    Image {
                        source: "qrc:/assets/bjdd.png"
                        width: 108; height: 108
                        anchors.left: parent.left
                        anchors.top: parent.top
                        anchors.topMargin: 0
                        fillMode: Image.PreserveAspectFit
                    }
                }
                Text { 
                    text: "电导"; color: "#333333"; font.pixelSize: 28; font.bold: true
                    anchors.top: parent.top
                    anchors.topMargin: 25
                }
            }

            ECheckBox {
                id: conductCheckBox
                showInputField: true
                textColor: "#2E394C"
                spacingBetweenBoxAndText: 6
                Layout.fillWidth: true
                Layout.minimumWidth: 450
                model: conductivityCheckModel
                onToggled: recomputeTop()
            }
        }
    }

    Item {
        id: printBtn
        width: 75
        height: 75
        anchors.bottom: parent.bottom
        anchors.right: parent.right
        anchors.rightMargin: 20
        anchors.bottomMargin: 20
        AnimatedImage {
            id: saveGif
            anchors.fill: parent
            source: "qrc:/assets/save-gift.gif"
            fillMode: Image.PreserveAspectFit
            playing: false
            cache: false
        }
        MouseArea {
            anchors.fill: parent
            onClicked: {
                saveGif.currentFrame = 0
                saveGif.playing = true
                const speed = motorCheckBox.getRowObject();
                const weight = weightCheckBox.getRowObject();
                const temperature = tempCheckBox.getRowObject();
                const ph = phCheckBox.getRowObject();
                const conductivity = conductCheckBox.getRowObject();
                alarmInfo.saveAlarmAll(speed,weight, temperature, ph, conductivity);
            }
        }
    }
}
